package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.ProductInventory;
import com.ruoyi.system.domain.Warehouse;
import com.ruoyi.system.enumeration.StockWarningEnum;
import com.ruoyi.system.mapper.ProductInventoryMapper;
import com.ruoyi.system.service.IProductInventoryService;
import com.ruoyi.system.service.IWarehouseService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.StoreIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品库存 服务实现类
 *
 * @author ruoyi
 * @date 2025-06-04
 */
@Service
public class ProductInventoryServiceImpl extends ServiceImpl<ProductInventoryMapper, ProductInventory> implements IProductInventoryService {

    @Autowired
    private StoreIdUtils storeIdUtils;

    @Autowired
    private IWarehouseService warehouseService;

    @Override
    public IPage<ProductInventory> page(ProductInventory productInventory) {
        Page<ProductInventory> page = new Page<>(productInventory.getPageNum(), productInventory.getPageSize());
        LambdaQueryWrapper<ProductInventory> wrapper = buildQueryWrapper(productInventory);
        IPage<ProductInventory> result = this.page(page, wrapper);
        if (AbdulEmptyUtils.isEmpty(result.getRecords())) {
            translate(page.getRecords());
        }
        return result;
    }

    @Override
    public List<ProductInventory> export(ProductInventory productInventory) {
        LambdaQueryWrapper<ProductInventory> wrapper = buildQueryWrapper(productInventory);
        List<ProductInventory> list = this.list(wrapper);
        if (!AbdulEmptyUtils.isEmpty(list)) {
            translate(list);
        }
        return list;
    }


    /**
     * 构建查询条件
     */
    public LambdaQueryWrapper<ProductInventory> buildQueryWrapper(ProductInventory productInventory) {
        LambdaQueryWrapper<ProductInventory> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getName()), ProductInventory::getName, productInventory.getName());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getWarehouseId()), ProductInventory::getWarehouseId, productInventory.getWarehouseId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getProductManagementId()), ProductInventory::getProductManagementId, productInventory.getProductManagementId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getInStock()), ProductInventory::getInStock, productInventory.getInStock());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getClassification()), ProductInventory::getClassification, productInventory.getClassification());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getCategory()), ProductInventory::getCategory, productInventory.getCategory());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getWarehouseId()), ProductInventory::getWarehouseId, productInventory.getWarehouseId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(productInventory.getStockWarning()), ProductInventory::getStockWarning, productInventory.getStockWarning());
        wrapper.eq(ProductInventory::getStoreId, storeIdUtils.getStoreId());
        wrapper.orderByDesc(ProductInventory::getUpdateTime);
        return wrapper;
    }

    /**
     * 数据翻译处理
     */
    public void translate(List<ProductInventory> productInventory) {
        LambdaQueryWrapper<Warehouse> query = Wrappers.lambdaQuery();
        query.select(Warehouse::getId, Warehouse::getWarehouseName);
        List<Warehouse> warehouseList = warehouseService.list(query);
        Map<Long, String> WarehouseMap = null;
        if (!AbdulEmptyUtils.isEmpty(warehouseList)) {
            WarehouseMap = warehouseList.stream().collect(Collectors.toMap(Warehouse::getId, Warehouse::getWarehouseName, (k1, k2) -> (String) k1));

        }
        for (ProductInventory record : productInventory) {
            Long warehouseId = record.getWarehouseId();
            if (!AbdulEmptyUtils.isEmpty(WarehouseMap)) {
                record.setWarehouseNameDesc(WarehouseMap.get(warehouseId));
            }
            record.setStockWarningDesc(StockWarningEnum.getValueByKey(record.getStockWarning()));
            record.setInStockDesc(record.getInStock().toString());
        }
    }
}
